package com.jiangyg.mall.coupon.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.utils.bean.BeanUtils;
import com.jiangyg.mall.coupon.constant.ColumnConstant;
import com.jiangyg.mall.coupon.dao.SeckillSkuDao;
import com.jiangyg.mall.coupon.dto.SeckillSkuDTO;
import com.jiangyg.mall.coupon.entity.SeckillSku;
import com.jiangyg.mall.coupon.vo.SeckillSkuVO;
import com.jiangyg.mall.db.mybatis.BaseServiceImpl;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SeckillSkuServiceImpl extends BaseServiceImpl<SeckillSkuDao, SeckillSku> implements SeckillSkuService {

    @Autowired
    public SeckillSkuServiceImpl(SeckillSkuDao seckillSkuDao) {
        super(seckillSkuDao);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public Pagination<SeckillSkuVO> selectPagination(Pagination<SeckillSkuVO> pagination) throws Exception {
        return this.executeSelectPagination(pagination, SeckillSkuVO.class);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public List<SeckillSkuVO> selectListBySessionId(Long sessionId) throws Exception {
        final QueryWrapper<SeckillSku> wrapper = new QueryWrapper<>();
        wrapper.eq(ColumnConstant.SESSION_ID, sessionId);
        final List<SeckillSku> dataList = this.baseMapper.selectList(wrapper);
        return BeanUtils.transformList(dataList, SeckillSkuVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(SeckillSkuDTO dto) throws Exception {
        return this.executeSaveOrUpdate(dto) > 0;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public SeckillSkuVO selectById(Long seckillSkuRelationId) throws Exception {
        return this.executeSelectById(seckillSkuRelationId, SeckillSkuVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Long> seckillSkuRelationIds) {
        return this.baseMapper.deleteBatchIds(seckillSkuRelationIds) > 0;
    }

}
